package cn.shen.edit.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.shen.common.util.WebResultUtil;
import cn.shen.common.vo.WebResult;
import cn.shen.dao.entity.User;
import cn.shen.dao.mapper.UserMapper;


import cn.shen.dao.vo.UserVo;
import cn.shen.edit.service.UserService;
import cn.shen.edit.util.JwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping
@Slf4j
public class LoginController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;




    @PostMapping("/login")
    public WebResult handleLogin(@RequestBody UserVo vo, HttpServletRequest request){
        String token = request.getHeader(JwtUtil.SUCCESS_TOKEN);
        if (ObjectUtil.isEmpty(token)){
            return userService.checkLogin(User.builder()
                    .username(vo.getUsername())
                    .password(vo.getPassword())
                    .power(vo.getPower())
                    .userid(vo.getUserid())
                    .build());
        }else {
            User tokenUser = JwtUtil.parseUserToke(token);
            return userService.checkLogin(tokenUser);
        }
    }

}

